package com.joysuch.wwyt.monitoringcenter.controller;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.entity.MhAlarmMessage;
import com.joysuch.wwyt.mh.entity.MhMonitoringPoint;
import com.joysuch.wwyt.mh.repository.MhAlarmMessageDao;
import com.joysuch.wwyt.mh.repository.MhMonitoringPointDao;
import com.joysuch.wwyt.mh.storage.IMonitorPointDataService;
import com.joysuch.wwyt.mh.storage.MonitorPointData;
import com.joysuch.wwyt.monitoringcenter.bean.*;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskDistrictEvaluation;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.joysuch.wwyt.risk.repository.RiskDistrictEvaluationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/monitoring-Point-center")
public class MonitoringPointCenterController {

    @Autowired
    private MhMonitoringPointDao mhMonitoringPointDao;
    @Autowired
    private MhAlarmMessageDao mhAlarmMessageDao;
    @Autowired
    private IMonitorPointDataService monitorPointDataService;
/*    @Autowired
    private MhMonitoringPointDao mhMonitoringPointDao*/;


    @RequestMapping("/mh-list")
    private Object getById(Long aid,Long cid,Long id, Long pid){
        DetailsPointDateBean detailsPointDateBean = new DetailsPointDateBean();
        detailsPointDateBean.addAir(this.monitorRealTimeDataBean(aid));
        detailsPointDateBean.addCamera(this.alarmRecordBean(cid));
        detailsPointDateBean.addDanger(this.monitoringPointInformationBean(id));
        detailsPointDateBean.addFacility(this.monitoringNumericalInformationBean(pid));
      //  System.out.println(this.monitorRealTimeDataBean(aid));
       // System.out.println(this.alarmRecordBean(cid));
       // System.out.println(this.monitoringPointInformationBean(id));
       // System.out.println(this.monitoringNumericalInformationBean(pid));
        return ResultBean.success(detailsPointDateBean);
    }


    //@RequestMapping("/mh-list")
    private MonitoringPointInformationBean monitoringPointInformationBean(Long id){
        MhMonitoringPoint one = mhMonitoringPointDao.getOne(id);
        MonitoringPointInformationBean monitoringPointInformationBean = new MonitoringPointInformationBean();
        monitoringPointInformationBean.setName(one.getName());
        //monitoringPointInformationBean.setName("name");
        monitoringPointInformationBean.setType(one.getMhMonitoringType().getName());
        //monitoringPointInformationBean.setFirstValue(10.0);
        monitoringPointInformationBean.setNumber(664200501);
        monitoringPointInformationBean.setSecondValue(15.0);
        monitoringPointInformationBean.setUnit(one.getUnit());
       // monitoringPointInformationBean.setUnit("unit");
        return monitoringPointInformationBean;
    }

    //监测实时数据
    private MonitorRealTimeDataBean monitorRealTimeDataBean(Long id){
        MhMonitoringPoint one = mhMonitoringPointDao.getOne(id);
        MonitorRealTimeDataBean monitorRealTimeDataBean = new MonitorRealTimeDataBean();
        monitorRealTimeDataBean.setUnit(one.getUnit());
        MonitorPointData monitorPointData = monitorPointDataService.realTimeVal(Context.getCurrentTenantId(), Context.getCompanyCode(), one.getPointCode()).orElse(null);
        if(monitorPointData == null){
            monitorRealTimeDataBean.setValue("");
        } else {
            monitorRealTimeDataBean.setValue(monitorPointData.getValue()+"");
        }
        return monitorRealTimeDataBean;
    }

    //监测数值信息
    private MonitoringNumericalInformationBean monitoringNumericalInformationBean(Long id){
        MhMonitoringPoint one = mhMonitoringPointDao.getOne(id);
        MonitoringNumericalInformationBean monitoringNumericalInformationBean = new MonitoringNumericalInformationBean();
        monitoringNumericalInformationBean.setTime(one.getUpdateTime());
        monitoringNumericalInformationBean.setUnit(one.getUnit());
        MonitorPointData monitorPointData = monitorPointDataService.realTimeVal(Context.getCurrentTenantId(), Context.getCompanyCode(), one.getPointCode()).orElse(null);
        if(monitorPointData == null){
            monitoringNumericalInformationBean.setValue("");
        } else {
            monitoringNumericalInformationBean.setValue(monitorPointData.getValue()+"");
        }
        return monitoringNumericalInformationBean;
    }

    //报警记录
    private AlarmRecordBean alarmRecordBean(Long id){
        MhAlarmMessage one = mhAlarmMessageDao.getOne(id);
        AlarmRecordBean alarmRecordBean = new AlarmRecordBean();
        alarmRecordBean.setLevel("17.3%");
        alarmRecordBean.setSolveStatus(one.getState()==0 ? "未处理" : "已处理" );
        alarmRecordBean.setSolveTime(one.getHandleTime());
        alarmRecordBean.setVlaue(one.getLevel()+"级");
        alarmRecordBean.setTime(one.getAlarmTime());
        return alarmRecordBean;
    }

    @Autowired
    private RiskDistrictDao riskDistrictDao;

    //风险分区列表
    @RequestMapping("/mh-risk-zoning")
    private List<RiskZoningBean> riskZoningBean(){
        List<RiskDistrict> all = riskDistrictDao.findAll();
        ArrayList<RiskZoningBean> list = new ArrayList<>();
        for (RiskDistrict r : all) {
            RiskZoningBean riskZoningBean = new RiskZoningBean();
            riskZoningBean.setId(r.getId());
            riskZoningBean.setName(r.getName());
            riskZoningBean.setLevel(r.getRiskLevel());
            riskZoningBean.setDangerCount(15);
            riskZoningBean.setStatus(r.getEvaluationState());
            riskZoningBean.setY(32.0545);
            riskZoningBean.setX(120.12345);
            list.add(riskZoningBean);
        }
        return list;
    }

   /* //区域风险程度统计
    @RequestMapping("mh-risk-statistics")
    private RiskStatisticsBean riskStatisticsBean(){

    }*/


    //区域风险程度计算
    private RiskStatisticsBean riskStatisticsBean(){
        RiskStatisticsBean riskStatisticsBean = new RiskStatisticsBean();
        riskStatisticsBean.setName("重大风险");
        riskStatisticsBean.setValue(15);
        return riskStatisticsBean;
    }


    @Autowired
    private RiskDistrictEvaluationDao riskDistrictEvaluationDao;

    //基本信息
    @RequestMapping("list")
    private RiskDistrictEvaluationBean riskDistrictEvaluationBean(Long id){
        RiskDistrictEvaluation one = riskDistrictEvaluationDao.getOne(id);
        RiskDistrictEvaluationBean r = new RiskDistrictEvaluationBean();
        r.setName("test");
        r.setNumber(one.getCode());
        r.setCompany(one.getResponsibleDepartId());
        r.setAreaDangerLevel(one.getStableRiskL());
        r.setDangerLevel(one.getStableRiskS());
        r.setPeople_res(one.getResponsiblePersonId());
        r.setPeople_assess(one.getEvaluationUserId());
        r.setTime_assess(one.getEvaluationTime());
        return r;
    }

    //监测点数值信息
    private ControlListBean controlListBean(Long id){
        ControlListBean c = new ControlListBean();
        /*c.setName();
        c.setLevel();
        c.setPeople();
        c.setType();
        c.setTime();*/
        return c;
    }

    //两单三卡
    private CardListBean cardListBean(){
        CardListBean cardListBean = new CardListBean();

        return cardListBean;
    }

    //listOne
    private listOneBean listOneBean(){
        listOneBean listOneBean = new listOneBean();


        return listOneBean;
    }
    //listTwo
    private listTwoBean listTwoBean(){
        listTwoBean listTwoBean = new listTwoBean();

        return listTwoBean;
    }










}
